package com.ruoyi.es;
import com.ruoyi.mq.kafka.CgCallable;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;
import java.io.IOException;
import	java.util.Random;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * <p>className: Test</p>
 * <p>description: </p>
 *
 * @author
 * @date 2019-08-27 20:08
 */
public class Test {

    public static void main(String[] args){

       /* test1();
        System.out.println("============test2===========");
        test2();*/

        //demoThread();
        ocrImg();
    }

    /**
     * 光学字符识别
     */
    public static void ocrImg(){
        ITesseract instance = new Tesseract();
        String path = "F://cg-images";


        File directory = new File(path);
        String courseFile = null;
        try {
            courseFile = directory.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //设置训练库的位置
        instance.setDatapath(courseFile + "//tessdata");
        //chi_sim ：简体中文， eng	根据需求选择语言库
        instance.setLanguage("eng");

        try {
            for(int i = 0;i < 8;i ++){
                File srcFile = new File(path + "//"+i+".png");
                long startTime = System.currentTimeMillis();
                String result =  instance.doOCR(srcFile);
                long endTime = System.currentTimeMillis();
                System.out.println("Time is：" + (endTime - startTime) + " 毫秒");
                System.out.println(i+",result: "+result);
            }

        } catch (TesseractException e) {
            e.printStackTrace();
        }



    }
    public static void demoThread(){
        ExecutorService pool = Executors.newFixedThreadPool(20);

        for(int i =0 ;i < 20 ; i ++){
            System.out.println(i+ "===start=="+System.currentTimeMillis());
            /*Future future = pool.submit(new Runnable() {
                @Override
                public void run() {
                    int next = new Random().nextInt(10);
                    try {
                        Thread.sleep(100L * next);
                    } catch (InterruptedException e) {
                        System.out.println("===============fore==Interrupted:"+next+","+System.currentTimeMillis());
                        return ;
                    }*//*finally {
                        System.out.println("===finally=end==:"+next+","+System.currentTimeMillis());
                    }*//*

                    System.out.println("===end==:"+next+","+System.currentTimeMillis());
                }
            });*/
            Future future = pool.submit(new CgCallable(String.valueOf(i),"",""));
            try {
                System.out.println(i+ "====callable:"+future.get(500, TimeUnit.MILLISECONDS).toString());
            } catch (InterruptedException e) {
                System.out.println("===============fore==InterruptedException:");
            } catch (ExecutionException e) {
                System.out.println("===============fore==ExecutionException:");
            } catch (TimeoutException e) {
                //任务执行超时则 取消该任务
                future.cancel(true);
                System.out.println(i+ "===============fore==TimeoutException:");
            }

            System.out.println(i+ ",last====="+System.currentTimeMillis());
        }


        pool.shutdown();
        System.out.println("shutdown====="+System.currentTimeMillis());
    }


    public static void test1(){
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(2);
        nums.add(7);
        nums.add(11);
        nums.add(15);

        System.out.println("src:"+nums);
        //找tar
        int tar = 18;
        int size = nums.size();
        int i = 0;
        int k = 0;
        boolean flagEx = false;
        for(;i < size;i ++){
            int t1 = nums.get(i).intValue();
            for(k = i+1 ;k < size ;k ++){
                int t2 = nums.get(k).intValue();
                if(tar == (t1 + t2)){
                    flagEx = true;
                    break;
                }
            }

            if(flagEx){
                break;
            }
        }
        if(flagEx){
            System.out.println("i:"+i+",k:"+k);
        }else{
            System.out.println("not fund");
        }
    }

    public static void test2(){

        List<Integer> nums = new ArrayList<>();
        nums.add(2);
        nums.add(7);
        nums.add(11);
        nums.add(15);
        System.out.println("src:"+nums);

        Map<Integer, Integer> map = new HashMap();
        for(int i = 0;i < nums.size(); i++){
            map.put(nums.get(i),i);
        }
        System.out.println("map:"+map);
        //找tar
        Integer tar = new Integer(18);
        int idxA = 0;
        int idxB = 1;

        for(int i = 0; i < nums.size(); i++) {
            Integer a = nums.get(i);
            if (map.containsKey(tar - a)) {
                idxA = i;
                idxB = map.get(tar - a) ;
                break;
            }
        }

        System.out.println("i:"+idxA+",k:"+idxB);
    }


}